<template>
  <el-dialog
    :title="'新增'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item>
        <el-popover
          ref="dtListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtTree"
            @current-change="dtListTreeCurrentChange"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
            style="width: 194px"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.dtName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择所属机构"
          style="width: 194px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataForm.blonPlantform"
          clearable
          @change="pfChange"
          placeholder="请选择销售平台"
        >
          <el-option
            v-for="item in pfs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.storeId" clearable placeholder="选择店铺">
          <el-option
            v-for="item in storeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dtList: [],
      storeList: [],
      pfs: [],
      dataForm: {
        blonPlantform: "",
        id: 0,
        dtId: "",
        dtName: "",
        storeId: "",
        storeName: "",
        appKey: "",
        status: "",
        date: "",
      },
      dtListTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  methods: {
    init(id) {
      this.getDtData();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/sysmq/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.dtId = data.sysMq.dtId;
              this.dataForm.dtName = data.sysMq.dtName;
              this.dataForm.storeId = data.sysMq.storeId;
              this.dataForm.storeName = data.sysMq.storeName;
              this.dataForm.appKey = data.sysMq.appKey;
              this.dataForm.status = data.sysMq.status;
              this.dataForm.date = data.sysMq.date;
            }
          });
        }
      });
    },

    getDtData() {
      this.$http({
        url: this.$http.adornUrl("/sys/mq/dt"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.pfs = data.spfs;
          if (data.dts.length > 0) {
            this.dataForm.dtId = data.dts[0].id;
          }
          this.dtList = treeDataTranslate(data.dts, "id");
          this.$nextTick(function () {
            this.dtListTreeSetCurrentNode();
          });
        }
      });
    },

    dtListTreeSetCurrentNode() {
      if (this.dataForm.dtId != null) {
        this.$refs.dtTree.setCurrentKey(this.dataForm.dtId);
        this.dataForm.dtName = (this.$refs.dtTree.getCurrentNode() || {})[
          "name"
        ];
      }
    },

    // 分销商树选中
    dtListTreeCurrentChange(data, node) {
      this.dataForm.dtId = data.id;
      this.dataForm.dtName = data.name;
      this.$nextTick(function () {
        this.getStore();
      });
    },
    pfChange(value) {
      this.getStore();
    },
    getStore() {
      this.$http({
        url: this.$http.adornUrl("/op/dtuphtl/store"),
        method: "get",
        params: this.$http.adornParams({
          dtId: this.dataForm.dtId,
          pfId: this.dataForm.blonPlantform,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.storeList = data.list;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/sys/mq/add"),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              dtId: this.dataForm.dtId,
              dtName: this.dataForm.dtName,
              storeId: this.dataForm.storeId,
              storeName: this.dataForm.storeName,
              appKey: this.dataForm.appKey,
              status: this.dataForm.status,
              date: this.dataForm.date,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
